<template>
  <div class="login-container">
    <!-- 背景 -->
    <div class="login-background"></div>

    <!-- 登录卡片 -->
    <div class="login-card">
      <h2 class="title">欢迎登录</h2>
      <p class="subtitle">请填写您的账户信息</p>

      <!-- 登录表单 -->
      <el-form
        ref="loginForm"
        :model="form"
        :rules="rules"
        class="login-form"
        @keyup.enter="submit"
      >
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input
            v-model="form.username"
            placeholder="请输入用户名/邮箱"
            clearable
          >
            <template #prefix>
              <el-icon><User /></el-icon>
            </template>
          </el-input>
        </el-form-item>

        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            type="password"
            placeholder="请输入密码"
            show-password
          >
            <template #prefix>
              <el-icon><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>


        <!-- 登录按钮 -->
        <el-form-item>
          <el-button
            type="primary"
            :loading="loading"
            @click="submit"
            style="width: 100%; height: 48px; font-size: 16px"
          >
            {{ loading ? '登录中...' : '登 录' }}
          </el-button>
        </el-form-item>
      </el-form>

    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { User, Lock, Avatar, Cellphone, Position } from '@element-plus/icons-vue'
import { userLoginApi } from '@/api/user.js'
// import router from '@/router/index.js'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'


const router = useRouter()
// 表单引用
const loginForm = ref([])

// 表单数据
const form = reactive({
  username: '',
  password: '', 
})

// 加载状态
const loading = ref(false)

// 表单验证规则
const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码至少6位', trigger: 'blur' }
  ]
}

// 提交登录
const submit =async () => {
  loginForm.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      // 模拟登录请求
      let result = await userLoginApi(form)
      if(result.code==1){
        loading.value = false
        ElMessage.success('登录成功！')
        localStorage.setItem('token',JSON.stringify(result.data.token))
        // 跳转页面
        router.push('/index')
      }else {
      ElMessage.error('请检查输入信息')
      return false
    }
}
  })
}


onMounted(() => {
})
</script>

<style scoped>
.login-container {
  position: relative;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: center;

}

.login-background {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: url('@/assets/images/login.png') no-repeat center center;
  background-size: cover;
  opacity: 0.7;
  z-index: 1;
}

.login-card {
  position: relative;
  z-index: 2;
  width: 400px;
  padding: 40px;
  background: #fff;
  border-radius: 16px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
}

.title {
  text-align: center;
  color: #333;
  margin-bottom: 10px;
  font-weight: 600;
  font-size: 24px;
}

.subtitle {
  text-align: center;
  color: #666;
  font-size: 14px;
  margin-bottom: 30px;
}

.login-form {
  margin-top: 20px;
}

.form-actions {
  display: flex;
  justify-content: space-between;
  font-size: 14px;
  margin-bottom: 20px;
}

.third-party {
  margin-top: 30px;
  text-align: center;
  color: #999;
}

.third-party .line {
  position: relative;
  margin: 20px 0;
  color: #ccc;
}
.third-party .line::before,
.third-party .line::after {
  content: '';
  position: absolute;
  top: 50%;
  width: 30%;
  height: 1px;
  background: #ddd;
}
.third-party .line::before {
  left: 0;
}
.third-party .line::after {
  right: 0;
}

.third-party .icons {
  display: flex;
  justify-content: center;
  gap: 20px;
  margin-top: 10px;
}

:deep(.el-input__prefix) {
  color: #999;
}

:deep(.el-button) {
  --el-button-bg-color: #409eff;
  --el-button-hover-bg-color: #66b1ff;
  --el-button-active-bg-color: #3a8ee6;
}
</style>